/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.dbfeatures;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.telosys.persistence.TelosysPersistenceException;
import org.telosys.persistence.dbfeatures.impl.DB2AS400Features;
import org.telosys.persistence.dbfeatures.impl.DB2Features;
import org.telosys.persistence.dbfeatures.impl.DerbyFeatures;
import org.telosys.persistence.dbfeatures.impl.H2Features;
import org.telosys.persistence.dbfeatures.impl.HSqlFeatures;
import org.telosys.persistence.dbfeatures.impl.InformixFeatures;
import org.telosys.persistence.dbfeatures.impl.IngresFeatures;
import org.telosys.persistence.dbfeatures.impl.MySqlFeatures;
import org.telosys.persistence.dbfeatures.impl.OracleFeatures;
import org.telosys.persistence.dbfeatures.impl.PostgreSqlFeatures;
import org.telosys.persistence.dbfeatures.impl.SqlServerFeatures;
import org.telosys.persistence.dbfeatures.impl.SybaseFeatures;
import org.telosys.persistence.dbfeatures.impl.SybaseSqlAnywhereFeatures;

public class DbFeaturesProvider {

	/**
	 * Returns the specific database features for the given meta-data
	 * @param databaseMetaData
	 * @return
	 */
	public DbFeatures getDbFeatures(DatabaseMetaData databaseMetaData) 
	{
		String databaseName;
		int databaseMajorVersion;
		int databaseMinorVersion;
		
		try {
			databaseName = databaseMetaData.getDatabaseProductName();
			
			databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
			databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
			
		} catch (SQLException e) {
			throw new TelosysPersistenceException("Cannot get database metadata information", e);
		}
		return getDbFeatures( databaseName, databaseMajorVersion, databaseMinorVersion) ;
		
	}			
	/**
	 * Returns the specific database features for the given database name and version
	 * @param databaseName
	 * @param databaseMajorVersion
	 * @param databaseMinorVersion
	 * @return
	 */
	public DbFeatures getDbFeatures(String databaseName, int databaseMajorVersion, int databaseMinorVersion) 
	{
		if ( "Oracle".equalsIgnoreCase( databaseName ) ) {
			return new OracleFeatures(databaseMajorVersion, databaseMinorVersion );
		}

		if ( "PostgreSQL".equalsIgnoreCase( databaseName ) ) {
            return new PostgreSqlFeatures(databaseMajorVersion, databaseMinorVersion );
		}

		if ( "MySQL".equalsIgnoreCase( databaseName ) ) {
			return new MySqlFeatures(databaseMajorVersion, databaseMinorVersion);
		}

		if ( "Apache Derby".equalsIgnoreCase( databaseName ) ) {
            return new DerbyFeatures(databaseMajorVersion, databaseMinorVersion );
		}

		if ( "HSQL Database Engine".equalsIgnoreCase( databaseName ) ) {
			return new HSqlFeatures(databaseMajorVersion, databaseMinorVersion);
		}

		if ( "H2".equalsIgnoreCase( databaseName ) ) {
			return new H2Features( databaseMajorVersion, databaseMinorVersion);
		}

		if ( "Ingres".equalsIgnoreCase( databaseName ) ) {
            return new IngresFeatures(databaseMajorVersion, databaseMinorVersion );
		}

		if ( databaseName.startsWith( "Microsoft SQL Server" ) ) {
            return new SqlServerFeatures(databaseMajorVersion, databaseMinorVersion );
		}

		if ( "Sybase SQL Server".equalsIgnoreCase( databaseName ) || "Adaptive Server Enterprise".equalsIgnoreCase( databaseName ) ) {
			return new SybaseFeatures(databaseMajorVersion, databaseMinorVersion);
		}

		if ( databaseName.startsWith( "Adaptive Server Anywhere" ) ) {
			return new SybaseSqlAnywhereFeatures(databaseMajorVersion, databaseMinorVersion);
		}

		if ( "Informix Dynamic Server".equalsIgnoreCase( databaseName ) ) {
			return new InformixFeatures(databaseMajorVersion, databaseMinorVersion);
		}
			
		if ( databaseName.equalsIgnoreCase("DB2 UDB for AS/400" ) ) {
			return new DB2AS400Features(databaseMajorVersion, databaseMinorVersion);
		}

		if ( databaseName.startsWith( "DB2/" ) ) {
			return new DB2Features(databaseMajorVersion, databaseMinorVersion);
		}

		return null;
	}
		
}
